psrc_census.R functions

These are instructions on how to use the functions in the library psrc_census.R

The code requires several libraries. The config file contains global values specific to PSRC. The main code is psrc_census.R.

Currently there are two functions:

You will need to source to files library/psrc_census_config.R and library/psrc_census.R. Make sure you enter the correct relative path to point to these files. For example, this code is running from the docs directory, so it needs to point one folder above as in ..//library

library(tidyverse)
library(sf)
library(leaflet)
library(tidycensus)
library(writexl)
source('../library/psrc_census_config.R')
source('../library/psrc_census.R')

Set up your API key

The first time you run this code, you will need to set our Census API Key as an environment variable, if you haven’t done that before. After that you can just get it. This is the website to get a key: https://api.census.gov/data/key_signup.html. Once you run Sys.setenv on the Census API Key you will only need to run Sys.getenv.

#Sys.setenv(CENSUS_API_KEY = 'PUT YOUR KEY HERE')
Sys.getenv("CENSUS_API_KEY")

Next you need to decide what tables you would like to download. This is the hardest part because you have find the correct table code, decide on geography, and which years.

Get the data —————————————————–

The main function psrc_acs_table has four parameters: tbl_code, geog, yr,acs.

You can find the list of ACS datasets available via the api here: https://www.census.gov/data/developers/data-sets.html

THe function to make a table is defined like this: psrc_table(tbl_code, geog, yr,acs)

Here are a few examples of using the function.

tract_asian<-psrc_acs_table("B02001_005", "tract", 2019,'acs5')
## Getting data from the 2015-2019 5-year ACS
## # A tibble: 776 x 10
##    GEOID  NAME    variable estimate   moe ACS_Year ACS_Type ACS_Geography label 
##    <chr>  <chr>   <chr>       <dbl> <dbl>    <dbl> <chr>    <chr>         <chr> 
##  1 53033~ Census~ B02001_~     1202   248     2019 acs5     tract         Estim~
##  2 53033~ Census~ B02001_~     1168   232     2019 acs5     tract         Estim~
##  3 53033~ Census~ B02001_~      484   155     2019 acs5     tract         Estim~
##  4 53033~ Census~ B02001_~      970   418     2019 acs5     tract         Estim~
##  5 53033~ Census~ B02001_~      465   194     2019 acs5     tract         Estim~
##  6 53033~ Census~ B02001_~      278   143     2019 acs5     tract         Estim~
##  7 53033~ Census~ B02001_~     1094   355     2019 acs5     tract         Estim~
##  8 53033~ Census~ B02001_~      997   254     2019 acs5     tract         Estim~
##  9 53033~ Census~ B02001_~      412   195     2019 acs5     tract         Estim~
## 10 53033~ Census~ B02001_~      323    64     2019 acs5     tract         Estim~
## # ... with 766 more rows, and 1 more variable: concept <chr>
county_ferry<-psrc_acs_table("B08006_013", "county", 2019, 'acs1')
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2019 1-year ACS
## # A tibble: 4 x 10
##   GEOID NAME   variable estimate   moe ACS_Year ACS_Type ACS_Geography label    
##   <chr> <chr>  <chr>       <dbl> <dbl>    <dbl> <chr>    <chr>         <chr>    
## 1 53033 King ~ B08006_~     1687   854     2019 acs1     county        Estimate~
## 2 53035 Kitsa~ B08006_~     9788  2076     2019 acs1     county        Estimate~
## 3 53053 Pierc~ B08006_~      118   124     2019 acs1     county        Estimate~
## 4 53061 Snoho~ B08006_~       61   106     2019 acs1     county        Estimate~
## # ... with 1 more variable: concept <chr>
region_female_under_5<-psrc_acs_table("B01001_027", "region", 2019, 'acs1')
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2019 1-year ACS
## `summarise()` has grouped output by 'variable', 'ACS_Year', 'ACS_Type'. You can override using the `.groups` argument.
## # A tibble: 1 x 6
## # Groups:   variable, ACS_Year, ACS_Type [1]
##   variable   total_region moe_region ACS_Year ACS_Type label                    
##   <chr>             <dbl>      <dbl>    <dbl> <chr>    <chr>                    
## 1 B01001_027       125373      1275.     2019 acs1     Estimate!!Total:!!Female~
#if you want you can write the data to the clipboard or out to csv or excel
write.table(county_ferry, "clipboard", sep="\t", row.names=FALSE)
write.csv(county_ferry, "ferry_workers_by_county.csv")
write_xlsx(county_ferry, "ferry_workers_by_county.xlsx")

Map the data —————————————————–

create_tract_map is function in which you send in a data frame by tract, and it makes a map. The parameter is takes in is one ACS variable by tract.

create_tract_map(tract_asian)
## Reading layer `dbo.tract2010_nowater' from data source `MSSQL:server=AWS-PROD-SQL\Sockeye;database=ElmerGeo;trusted_connection=yes' using driver `MSSQLSpatial'
## Simple feature collection with 773 features and 19 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 1099353 ymin: -97548.53 xmax: 1622631 ymax: 477101.5
## projected CRS:  NAD83 / Washington North (ftUS)